#include "loginwidget.h"
#include "phoneloginwidget.h"
#include <QDebug>
#include "mainwidget.h"

#include "toast.h"

LoginWidget::LoginWidget(QWidget* parent)
    :QWidget(parent), isLoginMode(true)
{

    //设置基本属性
    this->setFixedSize(400,400);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setStyleSheet("QWidget{background-color:rgb(255, 255, 255);}");
    this->setAttribute(Qt::WA_DeleteOnClose);

    tipLabel = new QLabel();
    tipLabel->setText("登录");
    tipLabel->setAlignment(Qt::AlignCenter);
    tipLabel->setFixedHeight(50);
    tipLabel->setStyleSheet("QLabel{font-size:40px;}");

    usernameEdit = new QLineEdit();
    usernameEdit->setPlaceholderText("输入用户名");
    usernameEdit->setFixedHeight(40);
    usernameEdit->setStyleSheet("QLineEdit{background-color:rgb(235, 235, 235); border:none; border-radius:5px; padding-left:10px; font-size: 20px;}");

    passwordEdit = new QLineEdit();
    passwordEdit->setPlaceholderText("输入密码");
    passwordEdit->setFixedHeight(40);
    passwordEdit->setStyleSheet("QLineEdit{background-color:rgb(235, 235, 235); border:none; border-radius:5px; padding-left:10px; font-size: 20px;}");
    passwordEdit->setEchoMode(QLineEdit::Password);

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setPlaceholderText("输入验证码");
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setStyleSheet("QLineEdit{background-color:rgb(235, 235, 235); border:none; border-radius:5px; padding-left:10px; font-size: 20px;}");

    //一定要设置验证码Edit逻辑为尽可能水平扩展, 因为水平还有一个QWidget
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //////////////////////////////////////////////////////////////////
    /// 等待后续实现验证码显示逻辑
    //////////////////////////////////////////////////////////////////
    verifyCodeWidget = new VerifyCodeWidget(this);

    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setStyleSheet("QPushButton{background-color:rgb(44, 182, 61); border:none; border-radius:5px;}");

    phoneModeBtn = new QPushButton();
    phoneModeBtn->setText("手机号登录");
    phoneModeBtn->setFixedHeight(40);
    phoneModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");

    switchModeBtn = new QPushButton();
    switchModeBtn->setText("注册");
    switchModeBtn->setFixedHeight(40);
    switchModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");

    ////////////////////////////////////////////////////
    /// 关联信号槽逻辑
    /// ////////////////////////////////////////////////
    connect(submitBtn,&QPushButton::clicked,this,&LoginWidget::clickSubmitBtn);
    connect(phoneModeBtn,&QPushButton::clicked,this, &LoginWidget::switchToPhone);
    connect(switchModeBtn,&QPushButton::clicked,this, &LoginWidget::switchMode);

    //添加各个控件到布局管理器中
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50,20,50,20);
    layout->setSpacing(10);
    layout->addWidget(tipLabel,0,0,1,5);
    layout->addWidget(usernameEdit,1,0,1,5);
    layout->addWidget(passwordEdit,2,0,1,5);
    layout->addWidget(verifyCodeEdit,3,0,1,2);
    layout->addWidget(verifyCodeWidget,3,2,1,3);
    layout->addWidget(submitBtn,4,0,1,5);
    layout->addWidget(phoneModeBtn,5,0,1,1);
    layout->addWidget(switchModeBtn,5,4,1,1);
    this->setLayout(layout);
}

void LoginWidget::clickSubmitBtn()
{
    //判断输入框内容是否为空

    const QString& username = usernameEdit->text();
    const QString& password = passwordEdit->text();
    const QString& code = verifyCodeEdit->text();

    if(username.isEmpty() || password.isEmpty())
    {
        Toast::showMessage("请输入用户名和密码!");
        return;
    }
    if(code.isEmpty())
    {
        Toast::showMessage("请输入验证码!");
        return;
    }
    if(!verifyCodeWidget->checkVerifyCode(code))
    {
        Toast::showMessage("验证码不一致, 请重新输入");
        return;
    }

    //发送网络请求, 如果是登录则发送登录请求, 否则发送注册请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(isLoginMode)
    {
        connect(dataCenter,&model::DataCenter::userLoginDone, this,&LoginWidget::userLoginDone);
        dataCenter->userLoginAsync(username,password);
    }
    else
    {
        connect(dataCenter,&model::DataCenter::userRegisterDone, this,&LoginWidget::userRegisterDone);
        dataCenter->userRegisterAsync(username,password);
    }

}

void LoginWidget::userLoginDone(bool ok, const QString& reason)
{
    if(ok)
    {
        MainWidget* w = MainWidget::getInstance();
        w->show();
    }
    else
    {
        Toast::showMessage("登录失败!, 失败原因: " + reason);
        return;
    }
}

void LoginWidget::userRegisterDone(bool ok, const QString& reason)
{
    if(ok)
    {
        Toast::showMessage("注册成功!");
        //密码和验证码输入框清空
        passwordEdit->setText("");
        verifyCodeEdit->setText("");
        //更新验证码
        verifyCodeWidget->refreshVerifyCode();
        switchMode();
    }
    else
    {
        Toast::showMessage("注册失败!, 失败原因: " + reason);
        return;
    }
}

void LoginWidget::switchMode()
{
    if(isLoginMode)
    {
        tipLabel->setText("注册");
        submitBtn->setText("注册");
        phoneModeBtn->setText("手机号注册");
        switchModeBtn->setText("登录");
    }
    else
    {
        tipLabel->setText("登录");
        submitBtn->setText("登录");
        phoneModeBtn->setText("手机号登录");
        switchModeBtn->setText("注册");
    }

    isLoginMode = !isLoginMode;
}

void LoginWidget::switchToPhone()
{
    PhoneLoginWidget* plw = new PhoneLoginWidget();
    plw->show();
    this->close();
}


